import time

from selenium import webdriver
from selenium.webdriver import Keys
from selenium.webdriver.common.by import By

# 打开 Chrome 浏览器
driver = webdriver.Chrome()

# 打开百度网址
driver.get("https://www.baidu.com/")

# 在搜索框中输入 "selenium" 并按 Enter 键
search_box = driver.find_element(By.ID, "kw")
search_box.send_keys("selenium")
enterBotton = driver.find_element(By.ID, "su")
enterBotton.click()

# 等待搜索结果加载
driver.implicitly_wait(10)

# 验证搜索结果页面至少显示10个搜索结果
search_results = driver.find_elements(By.CSS_SELECTOR, "h3.t a")
assert len(search_results) >= 10, "Less than 10 search results displayed"
if len(search_results) >= 10:
    print("页面显示了至少10个搜索结果")

# 验证第一个搜索结果链接来自Selenium官方网站
first_result_link = search_results[0].get_attribute("href")
if(first_result_link.startswith("https://www.selenium.dev/")):
    print("第一个结果是官网")
else:
    print("第一个结果不是官网")

# 验证搜索框文本包含 "selenium"
assert "selenium" in search_box.get_attribute("value"), "The search box text does not contain 'selenium'."
if "selenium" in search_box.get_attribute("value"):
    print("搜索框文本包含selenium")
else:
    print("搜索框文本不包含selenium")

time.sleep(5)

# 关闭浏览器
driver.quit()
